﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;
using SystemDiagramNew.Utils;

namespace HYOpeningDetail
{
	public class SlopeTag
	{
		public SlopeTag(Autodesk.Revit.DB.Document doc, XYZ ptLocation, XYZ ptDirection, double dSlopeValue)
		{
			this.m_doc = doc;
			this.m_ptLocation = ptLocation;
			this.m_ptDirection = ptDirection;
			this.m_dSlopeValue = dSlopeValue;
		}

		public void DrawSlopeTag()
		{
			try
			{
				this.MakingArcLine();
				this.DrawArc();
				this.DrawText();
			}
			catch (Exception)
			{
			}
		}

		private void MakingArcLine()
		{
			this.m_lstLine = new List<Line>();
			XYZ xyz = new XYZ(-this.m_ptDirection.Y, this.m_ptDirection.X, this.m_ptDirection.Z).Normalize();
			XYZ xyz2 = new XYZ(this.m_ptDirection.Y, -this.m_ptDirection.X, this.m_ptDirection.Z).Normalize();
			XYZ xyz3 = this.m_ptLocation.Add(NumbericTools.mmToFeet(this.m_dDirectionLineValue) * this.m_ptDirection);
			XYZ xyz4 = this.m_ptLocation.Add(NumbericTools.mmToFeet(this.m_dDirectionLineValue - this.m_dDistanceDirectionValue) * this.m_ptDirection);
			XYZ xyz5 = xyz4.Add(NumbericTools.mmToFeet(0.5 * this.m_dArcLineIntersectValue) * xyz);
			XYZ xyz6 = xyz4.Add(NumbericTools.mmToFeet(0.5 * this.m_dArcLineIntersectValue) * xyz2);
			this.m_lstLine.Add(Line.CreateBound(this.m_ptLocation, xyz3));
			this.m_lstLine.Add(Line.CreateBound(xyz3, xyz5));
			this.m_lstLine.Add(Line.CreateBound(xyz5, xyz6));
			this.m_lstLine.Add(Line.CreateBound(xyz6, xyz3));
		}

		private void DrawArc()
		{
			this.m_lstDetailCurve = new List<DetailCurve>();
			foreach (Line line in this.m_lstLine)
			{
				this.m_lstDetailCurve.Add(this.m_doc.Create.NewDetailCurve(this.m_doc.ActiveView, line));
			}
		}

		private void DrawText()
		{
			TextNoteType textNoteType = PublicFunction.GetTextType("仿宋_3.5mm", this.m_doc);
			if (textNoteType == null)
			{
				textNoteType = this.m_doc.CreateTextNoteType(3.5, 0.7, "仿宋");
			}
			TextNote textNote = TextNote.Create(this.m_doc, this.m_doc.ActiveView.Id, this.m_ptLocation, this.m_dSlopeValue.ToString("0.00") + "%", textNoteType.Id);
			this.m_doc.Regenerate();
			Autodesk.Revit.DB.View activeView = this.m_doc.ActiveView;
			double num = textNote.Width * (double)activeView.Scale;
			double num2 = textNote.Height * (double)activeView.Scale;
			XYZ coord = textNote.Coord;
			XYZ xyz = textNote.BaseDirection.Normalize();
			XYZ xyz2 = textNote.UpDirection.Normalize();
			XYZ xyz3 = this.m_ptLocation.Add(NumbericTools.mmToFeet(0.5 * this.m_dDirectionLineValue) * this.m_ptDirection);
			XYZ xyz4 = coord.Add(0.5 * num * xyz).Subtract(0.5 * num2 * xyz2);
			ElementTransformUtils.MoveElement(this.m_doc, textNote.Id, xyz3.Subtract(xyz4));
			this.m_doc.Regenerate();
			Line line = Line.CreateUnbound(xyz3, XYZ.BasisZ);
			double num3;
			if (-1.0 < this.m_ptDirection.Y && 1.0 >= this.m_ptDirection.Y && this.m_ptDirection.X >= 0.0)
			{
				num3 = this.m_ptDirection.AngleTo(new XYZ(1.0, 0.0, 0.0));
				if (0.0 > this.m_ptDirection.Y)
				{
					num3 = -num3;
				}
			}
			else
			{
				num3 = this.m_ptDirection.AngleTo(new XYZ(-1.0, 0.0, 0.0));
				if (0.0 <= this.m_ptDirection.Y)
				{
					num3 = -num3;
				}
			}
			ElementTransformUtils.RotateElement(this.m_doc, textNote.Id, line, num3);
			this.m_doc.Regenerate();
			double num4 = 1.0;
			XYZ xyz5 = textNote.UpDirection.Normalize();
			XYZ xyz6 = num4 * num2 * xyz5;
			ElementTransformUtils.MoveElement(this.m_doc, textNote.Id, xyz6);
			ElementSet elementSet = new ElementSet();
			foreach (DetailCurve detailCurve in this.m_lstDetailCurve)
			{
				elementSet.Insert(detailCurve);
			}
			elementSet.Insert(textNote);
			this.m_doc.CreatYJKGroup(elementSet);
		}

		private double m_dDirectionLineValue = 1100.0;

		private double m_dArcLineIntersectValue = 200.0;

		private double m_dDistanceDirectionValue = 300.0;

		private List<Line> m_lstLine;

		private List<DetailCurve> m_lstDetailCurve;

		private Document m_doc;

		private XYZ m_ptLocation;

		private XYZ m_ptDirection;

		private double m_dSlopeValue;
	}
}
